<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include './database/koneksi/Koneksi.php';

/**
 * Description of Tampil2
 *
 * @author Yosephdewa
 */
class Tampil2 {

    //put your code here
    public function TampilBarangTeratas() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang order by tgl_unggah desc limit 80");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilNamaBarang($id_barang) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select nama_barang from barang where id_barang = '" . mysql_real_escape_string($id_barang) . "'");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($result)) {
            $nama_barang = $row['nama_barang'];
        }
        return $nama_barang;
    }

    public function TampilUrlGambarBarang($id_barang) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select url_gambar_tumbnail from gambar where id_barang = '" . mysql_real_escape_string($id_barang) . "'");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($result)) {
            $url_gambar_tumbnail = $row['url_gambar_tumbnail'];
        }
        return $url_gambar_tumbnail;
    }

    public function TampilUrlGambarBarangUtama($id_barang) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select url_gambar_asli from gambar where id_barang = '" . mysql_real_escape_string($id_barang) . "'");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($result)) {
            $url_gambar_asli = $row['url_gambar_asli'];
        }
        return $url_gambar_asli;
    }

    public function TampilHasilCariBarang1($katakunci, $kategori, $provinsi) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where id_kategori = '"
                . mysql_real_escape_string($kategori) . "' and nama_barang like '%"
                . mysql_real_escape_string($katakunci) . "%' and id_provinsi = '"
                . mysql_real_escape_string($provinsi) . "' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilHasilCariBarang2($katakunci, $kategori) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where id_kategori = '"
                . mysql_real_escape_string($kategori) . "' or nama_barang like '%"
                . mysql_real_escape_string($katakunci) . "%' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilHasilCariBarang3($katakunci, $provinsi) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where nama_barang like '%"
                . mysql_real_escape_string($katakunci) . "%' and id_provinsi = '"
                . mysql_real_escape_string($provinsi) . "' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilHasilCariBarang4($kategori, $provinsi) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where id_kategori = '"
                . mysql_real_escape_string($kategori) . "' and id_provinsi = '"
                . mysql_real_escape_string($provinsi) . "' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilHasilCariBarang5($katakunci) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where nama_barang like '%"
                . mysql_real_escape_string($katakunci) . "%' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilHasilCariBarang6($kategori) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where id_kategori = '"
                . mysql_real_escape_string($kategori) . "' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilHasilCariBarang7($provinsi) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select id_barang from barang where id_provinsi = '"
                . mysql_real_escape_string($provinsi) . "' order by tgl_unggah desc");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function tampilKategori() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $rs = mysql_query("select * from kategori");
        if (!$rs) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($rs)) {
            $arrayData[$i++] = $row["nama_kategori"];
        }
        return $arrayData;
    }

    public function tampilIDKategori() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $rs = mysql_query("select * from kategori");
        if (!$rs) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($rs)) {
            $arrayData[$i++] = $row["id_kategori"];
        }


        return $arrayData;
    }

    public function tampilProvinsi() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $rs = mysql_query("select * from provinsi where id_provinsi != '00'");
        if (!$rs) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($rs)) {
            $arrayData[$i++] = $row["nama_provinsi"];
        }
        return $arrayData;
    }

    public function tampilIdProvinsi() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $rs = mysql_query("select * from provinsi where id_provinsi != '00'");
        if (!$rs) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($rs)) {
            $arrayData[$i++] = $row["id_provinsi"];
        }
        return $arrayData;
    }

    public function TampilBarangPengguna($email) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $rs1 = mysql_query("select id_pengguna from pengguna where email_pengguna ='"
                . mysql_real_escape_string($email) . "'");
        if (!$rs1) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($rs1)) {
            $result = $row["id_pengguna"];
        }
        $rs2 = mysql_query("select id_barang from barang where id_pengguna = '"
                . mysql_real_escape_string($result) . "' order by tgl_unggah desc limit 80");
        if (!$rs2) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($rs2)) {
            $arrayData[$i++] = $row['id_barang'];
        }
        return $arrayData;
    }

    public function TampilDataBarang($id_barang) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select * from barang where id_barang = '" . mysql_real_escape_string($id_barang) . "'");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[0] = $row['nama_barang'];
            $arrayData[1] = $row['deskripsi'];
            $arrayData[2] = $row['id_pengguna'];
        }
        return $arrayData;
    }

    public function TampilDataPengguna($id_pengguna) {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select * from pengguna where id_pengguna = '" . mysql_real_escape_string($id_pengguna) . "'");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        while ($row = mysql_fetch_array($result)) {
            $arrayData[0] = $row['nama_pengguna'];
            $arrayData[1] = $row['no_telp'];
            $arrayData[2] = $row['pin_BB'];
            $arrayData[3] = $row['alamat'];
        }
        return $arrayData;
    }

    public function get5Komentar($id_barang) {
        $kon = new Koneksi();
        $kon->koneksiDB();
        $query = mysql_query("select * from komentar where id_barang = " . mysql_real_escape_string($id_barang) . " order by tgl_komentar desc limit 5");
        if (!$query) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $i = 0;
        while ($row = mysql_fetch_array($query)) {
            $komentar = $row['komentar'];
            $nama = $this->getNamaPengguna($row['id_pengguna']);
            $commentshow .= "<div class=\"row\">"
                    . "<div class=\"col-lg-4\">"
                    . "<image width=\"80\" height=\"80\" class=\"img-responsive img-circle\" src=\"" . $this->getFotoPengguna($row['id_pengguna']) . "\"/>"
                    . "</div>"
                    . "<div class=\"col-lg-8\">"
                    . "<span><b> " . $nama . " </b><br/>" . $komentar . "</span>"
                    . "</div>"
                    . "</div>";
        }
        return $commentshow;
    }

    public function getIdPengguna($email) {
        $kon = new Koneksi();
        $kon->koneksiDB();
        $rs2 = mysql_query("select * from pengguna where email_pengguna = '" . mysql_real_escape_string($email) . "'");
        if (!$rs2) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($rs2)) {
            $id_pengguna = $row['id_pengguna'];
        }
        return $id_pengguna;
    }

    public function getNamaPengguna($id) {
        $kon = new Koneksi();
        $kon->koneksiDB();
        $rs2 = mysql_query("select * from pengguna where id_pengguna =" . mysql_real_escape_string($id));
        if (!$rs2) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($rs2)) {
            $nama_pengguna = $row['nama_pengguna'];
        }
        return $nama_pengguna;
    }

    public function getFotoPengguna($id) {
        $kon = new Koneksi();
        $kon->koneksiDB();
        $rs2 = mysql_query("select * from pengguna where id_pengguna =" . mysql_real_escape_string($id));
        if (!$rs2) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($rs2)) {
            $foto = $row['foto'];
        }
        return $foto;
    }

    public function tampilID_Login($email) {
        $id = "";
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $rs = mysql_query("select * from pengguna where email_pengguna = '" . mysql_real_escape_string($email) . "'");
        if (!$rs) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($rs)) {
            $id = $row["id_pengguna"];
        }

        return $id;
    }

    public function get3Testimoni() {
        $kon = new Koneksi();
        $kon->koneksiDB();
        $query = mysql_query("select * from testimoni order by tgl_testimoni desc limit 3");
        if (!$query) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $i = 0;
        while ($row = mysql_fetch_array($query)) {
            $testimoni = $row['pesan'];
            $foto = $this->getFotoPengguna($row['id_pengguna']);
            $testimonishow .= "<div class=\"row\">"
                    . "<div class=\"col-md-3 col-xs-3\">"
                    . "<img src=\"" . $foto . "\" class=\"img-thumbnail fotoBawah\"/>"
                    . "</div>"
                    . "<div class=\"col-md-9 col-xs-9\">"
                    . "<div class=\"well well-lg commentBawah\">"
                    . "<p>" . $testimoni . "</p>"
                    . "</div>"
                    . "</div>"
                    . "</div>";
        }
        return $testimonishow;
    }

}
